<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\util\File;
use app\appv110\model\SuperCardYuyue;
use app\appv110\model\SuperCard;

class ExperienceController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 90000  该预约不可服务，如有疑问请联系400-801-3680
         * 90001  服务确认码有误
         * 90002  确认失败
         * 90003  该体验卡活动还没有结束
         * 
         */
    }

    /*
     * 体验卡列表
     * author xfwed
     * date 2021.04.28
     */

    public function experienceList() {
        $rule = [
            'id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'type.require' => '分类必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $id = $all['id']; //美容师id/店铺id
        $type = $all['type']; //1店铺 2美容师
//        $storeIds = Db::table('sys_dictionary')
//                ->where('type', 'EXPERIENCE_STORE_IDS')
//                ->value('view');
//        $storeIds = explode(',', $storeIds);


        if ($type == 1) {
//            if (in_array($id, $storeIds)) {
//                $data[0]['id'] = 1;
//                $data[0]['name'] = '超值体验卡';
//                $data[0]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/tiyankaxiao.jpg';
//                $data[0]['special_money'] = '98000';//特权价
//                $data[0]['cost_money'] = '128000';//原价
//            } else {
//                $data = [];
//            }
            //查询店铺所在的城市
            $city_name = Db::name('store')
                    ->alias('s')
                    ->join('prov_city_area pca','pca.id = s.city')
                    ->where('s.id',$id)
                    ->value('pca.name');
            $data = Db::name('super_card')
                        ->where('end_time', '>', date('Y-m-d H:i:s'))
                        ->where('status',1)
                        ->where('find_in_set(:city_name,sell_city_name)', ['city_name' => $city_name])
                        ->field('id,name,price cost_money,special_price,img')
                        ->select();
            return Utils::ajaxReturn(0, '查询成功', $data);
        } elseif ($type == 2) {
//            $staff_store_ids = Db::name('staff_bind_store')
//                    ->where('staff_id', $id)
//                    ->column('store_id');
//            for ($i = 0; $i < count($staff_store_ids); $i++) {
//                if (in_array($staff_store_ids[$i], $storeIds)) {
//                    $data[0]['id'] = 1;
//                    $data[0]['name'] = '超值体验卡';
//                    $data[0]['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/tiyankaxiao.jpg';
//                    $data[0]['special_money'] = '98000';//特权价
//                    $data[0]['cost_money'] = '128000';//原价
//                }
//            }
//            $data = [];
//            return Utils::ajaxReturn(0, '查询成功', $data);
            $city_name = Db::name('staff')
                    ->alias('s')
                    ->join('prov_city_area pca','pca.id = s.city')
                    ->where('s.id',$id)
                    ->value('pca.name');
            $data = Db::name('super_card')
                            ->where('end_time', '>', date('Y-m-d H:i:s'))
                            ->where('status',1)
                            ->where('find_in_set(:city_name,sell_city_name)', ['city_name' => $city_name])
                            ->field('id,name,price cost_money,special_price,img')
                            ->select();
            return Utils::ajaxReturn(0, '查询成功', $data);
        }
    }

    /*
     * 体验卡详情
     * author xfwed 
     * date 2021.04.28
     * 
     */

    public function experienceInfo() {
//        $data['img'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/tiyankada.jpg';//图片
//        $data['special_money'] = '98000';//特权价
//        $data['cost_money'] = '128000';//原价

        $id = input('param.id');
        if (empty($id)) {
            $id = SuperCard::SUPERCARD_DEFAULT_ID;
        }
        $data = SuperCard::where('id', $id)
                ->field('id,name,price,special_price,sell_num,sell_num_base,content,img')
                ->find();

        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 生成体验卡详情二维码
     * author xfwed 
     * date 2021.04.28
     */

    public function experienceXcxCode() {
        $id = input('param.id'); //分享人ID
        $type = input('param.type'); //分享类型：1.美容师 2.店铺 3.用户
        $cId = input('param.cId'); //体验卡id
        //生成店铺小程序码
        $file = new File();
        $scene = 'id=' . $id . '&type=' . $type . '&cId=' . $cId;
        $page = 'packageC/pages/experienceCard/experienceCard';
        $xcxcode = $file->generateXcxPagecode($scene, $page);
        return Utils::ajaxReturn(0, '查询成功', $xcxcode);
    }

    /*
     * 体验卡订单列表
     * author xfwed 
     * date 2021.05.19
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function superCardOrderList() {
        $rule = [
            'store_id' => 'require',
            'status' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'status.require' => '分类必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $store_id = $all['store_id'];
        $status = $all['status']; //0全部2待服务3已完成

        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage; //->limit($offset, $perPage)
        if ($status) {
            $where['scy.status'] = $status;
        } else {
            $where['scy.status'] = ['in', '2,3'];
        }

        $where['scy.store_id'] = $store_id;
        $list = Db::name('super_card_yuyue')
                ->alias('scy')
                ->join('super_card_order sco', 'sco.id=scy.order_id', 'left')
                ->join('super_card_item sci', 'sci.id = scy.item_id', 'left')
                ->join('super_card sc', 'sc.id = sci.card_id', 'left')
                ->join('custom c', 'c.id = sco.ctm_id', 'left')
                ->order('scy.id desc')
                ->where($where)
                ->field('scy.id,scy.yuyue_date,scy.yuyue_time,scy.status,sco.order_no,sc.name,sc.img,sci.item_name,c.custom_name')
                ->limit($offset, $perPage)
                ->select();

        return Utils::ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 体验卡订单详情
     * author xfwed 
     * date 2021.05.19
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function superCardOrderInfo() {
        $rule = [
            'yuyue_id' => 'require',
            'pro_type' => 'require',
        ];
        $msg = [
            'yuyue_id.require' => '预约id必须',
            'pro_type.require' => '流水类型必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $pro_type = $all['pro_type']; //20是订单 21是预约
        $yuyue_id = $all['yuyue_id'];
        if ($pro_type == 20) {
            $where['sco.id'] = $yuyue_id;
            $row = Db::name('super_card_order')
                    ->alias('sco')
                    ->join('super_card sc', 'sc.id = sco.card_id', 'left')
                    ->join('custom c', 'c.id = sco.ctm_id', 'left')
                    ->where($where)
                    ->field('sco.id,sco.card_id,sco.order_no,sco.real_money,sco.pay_money,sco.balance_money,sco.red_money,sco.status,sco.num,sco.prepay_id,sco.pay_time,sco.add_time,sc.name,sc.img,sc.price,c.custom_phone,c.custom_name')
                    ->find();
        } elseif ($pro_type == 21) {
            $where['scy.id'] = $yuyue_id;
            $row = Db::name('super_card_yuyue')
                    ->alias('scy')
                    ->join('super_card_order sco', 'sco.id=scy.order_id', 'left')
                    ->join('super_card_item sci', 'sci.id = scy.item_id', 'left')
                    ->join('super_card sc', 'sc.id = sci.card_id', 'left')
                    ->join('custom c', 'c.id = sco.ctm_id', 'left')
                    ->join('store s', 's.id = scy.store_id', 'left')
                    ->where($where)
                    ->field('scy.id,scy.yuyue_date,scy.yuyue_time,scy.status,scy.note,sco.order_no,sco.pay_money,sco.pay_time,sc.name,sc.img,sci.item_name,c.custom_phone,c.custom_name,s.store_name')
                    ->find();
        }

        return Utils::ajaxReturn(0, '查询成功', $row);
    }

    /*
     * 确认服务完成
     * author xfwed 
     * date 2021.05.20
     * 
     * 1.修改预约表
     * 2.复制预约copy表
     * 3.修改订单状态
     * 4.分账 服务入账
     */

    public function verifySer() {
        $rule = [
            'yuyue_id' => 'require',
            'ser_code' => 'require',
        ];
        $msg = [
            'yuyue_id.require' => '预约id必须',
            'ser_code.require' => '服务完成码必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $yuyue_id = $all['yuyue_id']; //预约id
        $ser_code = $all['ser_code']; //服务完成码

        $superCardYuyueRow = Db::name('super_card_yuyue')
                ->where('id', $yuyue_id)
                ->find();
        $row = Db::name('super_card_order')
                ->where('id', $superCardYuyueRow['order_id'])
                ->find();
        $cardRow = Db::name('super_card')
                ->where('id', $row['card_id'])
                ->find();
        //判断是否可以服务完成
        if ($superCardYuyueRow['status'] != 2) {
            return Utils::ajaxReturn(90000, '该预约不可服务，如有疑问请联系400-801-3680', null);
        }
        //判断服务完成码是否正确
        if ($superCardYuyueRow['ser_code'] != $ser_code) {
            return Utils::ajaxReturn(90001, '服务确认码有误', null);
        }

        Db::startTrans();
        //1.修改预约状态
        $saveYuyueData['status'] = 3;
        $saveYuyueData['ser_time'] = date('Y-m-d H:i:s');
        $saveYuyue = Db::name('super_card_yuyue')
                ->where('id', $yuyue_id)
                ->update($saveYuyueData);
        //2.复制预约copy表
        $yuYueCopyData = Db::name('super_card_yuyue')
                ->where('id', $yuyue_id)
                ->field('id,add_time', TRUE)
                ->find();
        $yuYueCopyData['yuyue_id'] = $yuyue_id;
        $yuYueCopyData['add_time'] = date('Y-m-d H:i:s');
        $insertYuyueCopy = Db::name('super_card_yuyue_copy')
                ->insert($yuYueCopyData);
        //3.修改订单状态
        $saveOrder = SuperCardYuyue::saveOrderStatus($yuyue_id);
        //4.分账 服务入账（）
        $enjoy_id = $row['enjoy_id'];
        $enjoy_type = $row['enjoy_type']; // 分享类型：0平台 1.美容师 2.店铺 3.用户
        //4-1插入美容师服务流水
        if ($enjoy_type == 1) {
            $allmoney = $superCardYuyueRow['money'];
            $flowData['order_id'] = $yuyue_id;
            $flowData['item_name'] = '体验卡-' . $cardRow['name'];
            $flowData['type'] = 1;
            $flowData['pro_type'] = 21;
            $flowData['staff_id'] = $enjoy_id;
            $flowData['all_money'] = $allmoney;
            $flowData['money'] = $allmoney * ($superCardYuyueRow['staff_per'] / 100);
            $flowData['ctm_id'] = $row['ctm_id'];
            $flowData['ctm_name'] = Db::name('custom')->where('id', $row['ctm_id'])->value('custom_name');
            $flowData['ctm_photo'] = Db::name('custom')->where('id', $row['ctm_id'])->value('custom_photo');
            $flowData['status'] = 0;
            $flowData['add_time'] = date('Y-m-d H:i:s');
            $insertStaffFlow = Db::name('staff_flow_new')->insert($flowData);
        } else {//不是美容师销售的不插入服务流水
            $insertStaffFlow = TRUE;
        }
        //4-2插入店铺的服务流水
        $allmoney = $superCardYuyueRow['money'];
        $last_money = Db::name('store_flow_copy')->where('store_id', $superCardYuyueRow['store_id'])->order('id desc')->value('last_money');
        $last_money = $last_money + $allmoney * ($superCardYuyueRow['store_per'] / 100);
        $storeflowData['order_id'] = $yuyue_id;
        $storeflowData['item_name'] = '体验卡-' . $cardRow['name'];
        $storeflowData['type'] = 1;
        $storeflowData['pro_type'] = 21;
        $storeflowData['store_id'] = $superCardYuyueRow['store_id'];
        $storeflowData['allmoney'] = $allmoney;
        $storeflowData['money'] = $allmoney * ($superCardYuyueRow['store_per'] / 100);
        $storeflowData['last_money'] = $last_money;
        $storeflowData['add_time'] = date('Y-m-d H:i:s');
        $insertStoreFlow = Db::name('store_flow_copy')->insert($storeflowData);
        if ($saveYuyue && $insertYuyueCopy && $saveOrder && $insertStaffFlow && $insertStoreFlow) {
            Db::commit();
            return Utils::ajaxReturn(0, '确认成功', null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(90002, '确认失败', null);
        }
    }

    /*
     * 活动结束
     * author xfwed 
     * date 2021.05.20
     * 
     * 1.修改预约表
     * 2.复制预约copy表
     * 3.修改订单状态
     * 4.分账 
     * 插入店铺销售入账和服务入账（只插流水表不插copy） 
     * 修改美容师的销售入账和服务入账 
     * 插入平台销售流水  
     * 插入平台服务流水
     * 插入平台的沉淀流水（包括没有服务的订单和销售不是美容师的美容师服务流水）
     */

    public function activityEnd() {
        $rule = [
            'card_id' => 'require',
        ];
        $msg = [
            'card_id.require' => '体验卡id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $card_id = $all['card_id']; //体验卡id

        $cardRow = Db::name('super_card')
                ->where('id', $card_id)
                ->find();
        //判断体验卡是否已经到期
        $end_time = strtotime($cardRow['end_time']) + 24 * 60 * 60;
        if ($end_time > time()) {
            return Utils::ajaxReturn(90003, '该体验卡活动还没有结束', null);
        }
        //查询购买该体验卡的未完成的订单
        $orderList = Db::name('super_card_order')
                ->where('card_id', $card_id)
                ->where('status', 'in', '2,3')
                ->select();
        if(empty($orderList)){
             $a = TRUE;
             $b = TRUE;
             $c = TRUE;
        }
        foreach ($orderList as $k => $v) {
            $order_id = $v['id'];
            //查询没有完成的预约
            $yuyueList = Db::name('super_card_yuyue')
                    ->where('order_id', $order_id)
                    ->where('status', '<>', 3)
                    ->select();
            if (empty($yuyueList)) {
                $a = TRUE;
            }
            foreach ($yuyueList as $key => $val) {
                Db::startTrans();
                //1.修改预约表
                $saveYuyueData['status'] = 4;
                $saveYuyue = Db::name('super_card_yuyue')
                        ->where('id', $val['id'])
                        ->update($saveYuyueData);
                //2.复制预约copy表
                $yuYueCopyData = Db::name('super_card_yuyue')
                        ->where('id', $val['id'])
                        ->field('id,add_time', TRUE)
                        ->find();
                $yuYueCopyData['yuyue_id'] = $val['id'];
                $yuYueCopyData['add_time'] = date('Y-m-d H:i:s');
                $insertYuyueCopy = Db::name('super_card_yuyue_copy')
                        ->insert($yuYueCopyData);
                //3.修改订单状态
                $saveOrder = SuperCardYuyue::saveOrderStatus($val['id']);
                if ($saveYuyue && $insertYuyueCopy && $saveOrder) {
                    $a = TRUE;
                    Db::commit();
                } else {
                    $a = FALSE;
                    Db::rollback();
                }
            }
        }

        //4.分账 
        //* 插入店铺销售入账和服务入账（只插流水表不插copy） 
        //* 修改美容师的销售入账和服务入账 
        //* 插入平台销售流水  
        //* 插入平台服务流水
        //* 插入平台的沉淀流水（包括没有服务的订单和销售不是美容师的美容师服务流水）
        //查询购买该体验卡的订单
        $endOrderList = Db::name('super_card_order')
                ->where('card_id', $card_id)
                ->where('status', 4)
                ->select();
        foreach ($endOrderList as $k => $v) {
            Db::startTrans();
            //销售入账
            $enjoy_id = $v['enjoy_id'];
            $enjoy_type = $v['enjoy_type'];
            $card_id = $v['card_id'];
            $order_id = $v['id'];
            $cardRow = Db::name('super_card')
                    ->where('id', $card_id)
                    ->find();
            if ($enjoy_type == 0) {//0平台 1.美容师 2.店铺 3.用户
                //平台的销售流水  在最终活动结束分账的时候才插入
                $kkmFlowRow = Db::name('kkm_flow')
                        ->where('order_id', $v['id'])
                        ->where('pro_type', 20)
                        ->find();
                if ($kkmFlowRow) {
                    $insertFlow = TRUE;
                    $saveMoney = TRUE;
                } else {
                    $allmoney = $v['real_money'] - $v['red_money'];
                    $last_money = Db::name('kkm_flow')->order('id desc')->value('last_money');
                    $last_money = $last_money + $allmoney * ($cardRow['sell_per'] / 100);
                    $flowData['order_id'] = $v['id'];
                    $flowData['item_name'] = '体验卡-' . $cardRow['name'];
                    $flowData['type'] = 1;
                    $flowData['pro_type'] = 20;
                    $flowData['allmoney'] = $allmoney;
                    $flowData['money'] = $allmoney * ($cardRow['sell_per'] / 100);
                    $flowData['last_money'] = $last_money;
                    $flowData['add_time'] = date('Y-m-d H:i:s');
                    $insertFlow = Db::name('kkm_flow')->insert($flowData);
                    $saveMoney = TRUE;
                }
            } elseif ($enjoy_type == 1) {
                $staffFlowRow = Db::name('staff_flow_new')
                        ->where('order_id', $v['id'])
                        ->where('pro_type', 20)
                        ->find();
                if ($staffFlowRow['status'] == 2) {
                    $insertFlow = TRUE;
                    $saveMoney = TRUE;
                } else {
                    $flowData1['status'] = 2;
                    $flowData1['update_time'] = date('Y-m-d H:i:s');
                    $insertFlow = Db::name('staff_flow_new')
                            ->where('order_id', $v['id'])
                            ->where('pro_type', 20)
                            ->update($flowData1);
                    $money = Db::name('staff_flow_new')
                            ->where('order_id', $v['id'])
                            ->where('pro_type', 20)
                            ->value('money');
                    $saveMoney = Db::name('staff')->where('id', $enjoy_id)->setInc('money', $money);
                }
            } elseif ($enjoy_type == 2) {
                $storeFlowRow = Db::name('store_flow')
                        ->where('order_id', $v['id'])
                        ->where('pro_type', 20)
                        ->find();
                if ($storeFlowRow) {
                    $insertFlow = TRUE;
                    $saveMoney = TRUE;
                } else {
                    $allmoney = $v['real_money'] - $v['red_money'];
                    $last_money = Db::name('store_flow')->where('store_id', $enjoy_id)->order('id desc')->value('last_money');
                    $last_money = $last_money + $allmoney * ($cardRow['sell_per'] / 100);
                    $flowData['order_id'] = $v['id'];
                    $flowData['item_name'] = '体验卡-' . $cardRow['name'];
                    $flowData['type'] = 1;
                    $flowData['pro_type'] = 20;
                    $flowData['store_id'] = $enjoy_id;
                    $flowData['allmoney'] = $allmoney;
                    $flowData['money'] = $allmoney * ($cardRow['sell_per'] / 100);
                    $flowData['last_money'] = $last_money;
                    $flowData['add_time'] = date('Y-m-d H:i:s');
                    $insertFlow = Db::name('store_flow')->insert($flowData);
                    $money = $allmoney * ($cardRow['sell_per'] / 100);
                    $saveMoney = Db::name('store')->where('id', $enjoy_id)->setInc('money', $money);
                }
            }
            if ($insertFlow && $saveMoney) {
                $b = TRUE;
                Db::commit();
            } else {
                $b = FALSE;
                Db::rollback();
            }

            //服务入账 沉淀入账
            $endYuyueList = Db::name('super_card_yuyue')
                    ->where('order_id', $order_id)
                    //->where('status', 3)
                    ->where('is_account', 2)
                    ->select();
            if (empty($endYuyueList)) {
                $c = TRUE;
            }
            foreach ($endYuyueList as $key => $val) {
                Db::startTrans();
                $status = $val['status']; //3服务完成 4活动结束的
                if ($status == 3) {
                    //插入美容师服务流水
                    if ($enjoy_type == 1) {
                        $flowData2['status'] = 2;
                        $flowData2['update_time'] = date('Y-m-d H:i:s');
                        $insertStaffFlow = Db::name('staff_flow_new')
                                ->where('order_id', $val['id'])
                                ->where('pro_type', 21)
                                ->update($flowData2);
                        $money = Db::name('staff_flow_new')
                                ->where('order_id', $val['id'])
                                ->where('pro_type', 21)
                                ->value('money');
                        $saveStaffSerMoney = Db::name('staff')->where('id', $enjoy_id)->setInc('money', $money);
                        $insertKkmFlow = TRUE;
                    } else {//不是美容师销售的不插入服务流水 插入平台沉淀
                        $insertStaffFlow = TRUE;
                        $saveStaffSerMoney = TRUE;
                        $allmoney = $val['money'];
                        $last_money = Db::name('kkm_flow')->order('id desc')->value('last_money');
                        $last_money = $last_money + $allmoney * ($val['staff_per'] / 100);
                        $flowData['order_id'] = $val['id'];
                        $flowData['item_name'] = '体验卡(美容师服务分佣)-' . $cardRow['name'];
                        $flowData['type'] = 1;
                        $flowData['pro_type'] = 22;
                        $flowData['allmoney'] = $allmoney;
                        $flowData['money'] = $allmoney * ($val['staff_per'] / 100);
                        $flowData['last_money'] = $last_money;
                        $flowData['add_time'] = date('Y-m-d H:i:s');
                        $insertKkmFlow = Db::name('kkm_flow')->insert($flowData);
                    }
                    //插入店铺的服务流水
                    $allmoney = $val['money'];
                    $last_money = Db::name('store_flow')->where('store_id', $val['store_id'])->order('id desc')->value('last_money');
                    $last_money = $last_money + $allmoney * ($val['store_per'] / 100);
                    $storeflowData['order_id'] = $val['id'];
                    $storeflowData['item_name'] = '体验卡-' . $cardRow['name'];
                    $storeflowData['type'] = 1;
                    $storeflowData['pro_type'] = 21;
                    $storeflowData['store_id'] = $val['store_id'];
                    $storeflowData['allmoney'] = $allmoney;
                    $storeflowData['money'] = $allmoney * ($val['store_per'] / 100);
                    $storeflowData['last_money'] = $last_money;
                    $storeflowData['add_time'] = date('Y-m-d H:i:s');
                    $insertStoreFlow = Db::name('store_flow')->insert($storeflowData);
                    $money = $allmoney * ($val['store_per'] / 100);
                    $saveStoreSerMoney = Db::name('store')->where('id', $val['store_id'])->setInc('money', $money);
                    //插入平台的服务流水
                    $allmoney = $val['money'];
                    $last_money = Db::name('kkm_flow')->order('id desc')->value('last_money');
                    $last_money = $last_money + $allmoney * ($val['kkm_per'] / 100);
                    $flowData['order_id'] = $val['id'];
                    $flowData['item_name'] = '体验卡-' . $cardRow['name'];
                    $flowData['type'] = 1;
                    $flowData['pro_type'] = 21;
                    $flowData['allmoney'] = $allmoney;
                    $flowData['money'] = $allmoney * ($val['kkm_per'] / 100);
                    $flowData['last_money'] = $last_money;
                    $flowData['add_time'] = date('Y-m-d H:i:s');
                    $insertKkmSerFlow = Db::name('kkm_flow')->insert($flowData);

                    $insertKkmFlow1 = TRUE;
                } elseif ($status == 4) {
                    $insertStaffFlow = TRUE;
                    $saveStaffSerMoney = TRUE;
                    $insertKkmFlow = TRUE;
                    $insertStoreFlow = TRUE;
                    $saveStoreSerMoney = TRUE;
                    $insertKkmSerFlow = TRUE;
                    $allmoney = $val['money'];
                    $last_money = Db::name('kkm_flow')->order('id desc')->value('last_money');
                    $last_money = $last_money + $allmoney * (1 - ($val['sell_per'] / 100));
                    $flowData['order_id'] = $val['id'];
                    $flowData['item_name'] = '体验卡(活动结束)-' . $cardRow['name'];
                    $flowData['type'] = 1;
                    $flowData['pro_type'] = 22;
                    $flowData['allmoney'] = $allmoney;
                    $flowData['money'] = $allmoney * (1 - ($val['sell_per'] / 100));
                    $flowData['last_money'] = $last_money;
                    $flowData['add_time'] = date('Y-m-d H:i:s');
                    $insertKkmFlow1 = Db::name('kkm_flow')->insert($flowData);
                }
                $saveYuyueIsAccount = Db::name('super_card_yuyue')
                        ->where('id', $val['id'])
                        ->update(['is_account' => 1]);
                if ($insertStaffFlow && $saveStaffSerMoney && $insertKkmFlow && $insertStoreFlow && $saveStoreSerMoney && $insertKkmFlow1 && $saveYuyueIsAccount && $insertKkmSerFlow) {
                    $c = TRUE;
                    Db::commit();
                } else {
                    $c = FALSE;
                    Db::rollback();
                }
            }
        }
        if ($a && $b && $c) {
            return Utils::ajaxReturn(0, '成功', null);
        } else {
            return Utils::ajaxReturn(0, '活动结束失败', $a . '+' . $b . '+' . $c);
        }
    }

}
